package com.voice.client.message.entity;

import lombok.Data;

@Data
public class Message {

    private static final int QOS0 = 0;
    private static final int QOS1 = 1;
    private static final int QOS2 = 2;
    private byte[] payload;
    private int messageId;
    private String topic;
    private int qos;
    private long generateTime;

    public void setQos(int qos) {
        if (qos >= 0 && qos <= 2) {
            this.qos = qos;
        } else {
            throw new IllegalArgumentException();
        }
    }

    public Message(byte[] payload,int messageId,int qos) {
        this.payload = payload;
        this.messageId = messageId;
        this.generateTime=System.currentTimeMillis();
    }

    public Message(byte[] payload, int messageId,int qos,long generateTime) {
        this.payload = payload;
        this.messageId = messageId;
        this.qos = qos;
        this.generateTime=generateTime;
    }

    public Message(String topic,int messageId,int qos) {
        this.topic=topic;
        this.messageId = messageId;
        this.qos = qos;
        this.generateTime=System.currentTimeMillis();
    }

    public Message(String topic,int messageId,int qos, long generateTime) {
        this.topic=topic;
        this.messageId = messageId;
        this.qos = qos;
        this.generateTime=generateTime;
    }

    public Message(byte[] payload, String topic, int messageId, int qos) {
        this.payload = payload;
        this.topic = topic;
        this.messageId = messageId;
        this.qos = qos;
        this.generateTime =System.currentTimeMillis();
    }

    public Message(byte[] payload, String topic, int messageId, int qos, long generateTime) {
        this.payload = payload;
        this.topic = topic;
        this.messageId = messageId;
        this.qos = qos;
        this.generateTime = generateTime;
    }

    public String toString() {
        if(this.payload==null) return "Message{topic='" + this.topic + '\'' + ", messageId='" + this.messageId + '\'' + ", qos=" + this.qos + ", generateTime=" + this.generateTime + '}';
        return "Message{payload=" + new String(this.payload) + ", topic='" + this.topic + '\'' + ", messageId='" + this.messageId + '\'' + ", qos=" + this.qos + ", generateTime=" + this.generateTime + '}';
    }

    public boolean needAck() {
        return qos == 1 || qos == 2;
    }
}
